package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.CommentConverter;
import com.dycx.user.dto.CommentDTO;
import com.dycx.user.entity.CommentDO;
import com.dycx.user.mapper.CommentMapper;
import com.dycx.user.service.CommentService;
import com.dycx.user.service.UserInfoService;
import com.dycx.user.vo.CommentVO;
import com.dycx.user.vo.UserInfoVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 评论(Comment)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-12-29 10:53:25
 */

@Service
@AllArgsConstructor
public class CommentImpl extends ServiceImpl<CommentMapper, CommentDO> implements CommentService {

    private CommentMapper commentMapper;
    private UserInfoService userInfoService;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<CommentVO> getPage(CommentDTO dto) {
        LambdaQueryWrapper<CommentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getCommentId()), CommentDO::getCommentId, dto.getCommentId());
        wrapper.eq(ObjectUtil.isNotNull(dto.getBizType()), CommentDO::getBizType, dto.getBizType());
        wrapper.eq(ObjectUtil.isNotNull(dto.getBizId()), CommentDO::getBizId, dto.getBizId());
        wrapper.eq(ObjectUtil.isNotNull(dto.getStatus()), CommentDO::getStatus, dto.getStatus());
        wrapper.orderByDesc(CommentDO::getCommentId);

        Page<CommentDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<CommentVO> voPage = CommentConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @param commentId 主键
     * @return 查询结果
     */
    @Override
    public CommentVO load(Integer commentId) {
        CommentDO entity = getById(commentId);

        return setData(CommentConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public CommentVO setData(CommentVO vo) {
        UserInfoVO userInfoVO = userInfoService.load(vo.getUid());
        if (ObjectUtil.isNotNull(userInfoVO)) {
            vo.setNickname(userInfoVO.getNickname());
        }
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(CommentDTO dto) {
        CommentDO entity = BeanUtil.toBean(dto, CommentDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(CommentDTO dto) {
        CommentDO entity = BeanUtil.toBean(dto, CommentDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param commentId 主键
     * @return
     */
    @Override
    public Boolean delete(Integer commentId) {
        return removeById(commentId);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return commentMapper.getMaxSort();
    }
}

